#!/usr/bin/env bash

#get this script real directory
SOURCE="$0"
while [ -h "$SOURCE"  ]; do # resolve $SOURCE until the file is no longer a symlink
    this_script_dir="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
    SOURCE="$(readlink "$SOURCE")"
    # if $SOURCE was a relative symlink, 
    #we need to resolve it relative to the path 
    #where the symlink file was located
    [[ $SOURCE != /*  ]] && SOURCE="$this_script_dir/$SOURCE"
done
this_script_dir="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
source $this_script_dir/global.sh

#$this_script_dir/install_expect.sh
if [ -f ~/.profile ] ; then
    source ~/.profile
fi

################################################################################
#                                                                          80col
print_help() 
{
    cat <<EOF
    useage: 
    $0 --src_username=<root> --src_ip --src_path --dst_username ...


    --src_username=<root>   - the name of src host
    --src_ip=<ip>           - src host's ip
    --src_passwd=<passwd>   - src user's login passworld
    --src_port=<port>       - src host's port of  open ssh (default 22) 
    --src_path=<path>       - src host's port of  open ssh (default 22) 

    --dst_username=<root>   - the name of dst host
    --dst_ip=<ip>           - dst host's ip
    --dst_passwd=<passwd>   - dst host user's login passworld
    --dst_port=<port>       - dst host's port of  open ssh (default 22) 
    --dst_path=<path>       - dst host's port of  open ssh (default 22) 

    --log_level=<debug> - control pint log on streen,trace|debug|info|warn|error|critical

    --help  - prints help screen, script will exit after printing


EOF
}

#parse_arguments "$@" #must have "" on $@ ,or result in wrong num of args in function

if [ -n "$help" ] || [ $# -eq 0 ]; then
    print_help
    exit 0
fi

#1:username 
#2:host_ip
#3:passwd
#4:ssh_port
#5:cmd
#6:show_result

#check_argument username host_ip cmd || exit 1
if [ -z "$src_username" ] && [ -z "$dst_username" ] ; then
    check_argument src_username dst_username || exit 1
fi

if [ -n "$src_username" ]  ; then 
    check_argument src_username src_ip src_path || exit 1
fi
if [ -n "$dst_username" ] ; then 
    check_argument dst_username dst_ip dst_path || exit 1
fi

if [ -z "$src_port" ]; then
    src_port=22
fi

if [ -z "$dst_port" ]; then
    dst_port=22
fi

check_ip_valid $src_ip $dst_ip || exit 1

which expect > /dev/null
if [ $? -ne 0 ] ; then
    vlogc "error: expect is not installed, pleased install it by sudo apt install -y expect"
    exit 1
fi

#${passwd//$/\\$} \  #replace $ to \$

scp_copy_file()
{
    local src=$1
    local dst=$2
    local port=$3
    local passwd=$4

    src=${src/#\~/$HOME}
    dst=${dst/#\~/$HOME}

    if [ -z "$src" ] || [ -z "$dst" ] ; then
        return 1
    fi

    vlogi "start rsync copy: $src -> $dst"

    if should_log debug ; then
        $this_script_dir/vrsync.exp "$src" "$dst" $port ${passwd}
    else
        #echo "$this_script_dir/vrsync.exp $src $dst $port ${passwd}"
        $this_script_dir/vrsync.exp "$src" "$dst" $port ${passwd} >> $log_file 2>&1
    fi

    ret=$?

    #log out some error message about failed
    if [ $ret -ne 0 ] ; then 
        if [ $ret -eq 3 ] ; then
            reason_str="Permission denied passwd not right"
        elif [ $ret -eq 130 ] ; then
            reason_str="user interrupt execution by ctrl-c"
        elif [ ! -e "$src" ] ; then
            reason_str="src file doest not exist, src:$src "
        else
            reason_str="please check ip/port"
        fi

        vloge "rsync failed," \
            "return:$ret; "   \
            "reason:\"$reason_str\"; " \
            "cmd is:\" rsync -acz --delete -e \"ssh -p $port -o ConnectTimeout=5 -o StrictHostKeyChecking=no\" $src $dst\""
    fi

    return $ret
}

    #--src_username=<root>   - the name of src host
    #--src_ip=<ip>           - src host's ip
    #--src_passwd=<passwd>   - src user's login passworld
    #--src_port=<port>       - src host's port of  open ssh (default 22) 
    #--src_path=<path>       - src host's port of  open ssh (default 22) 

    #--dst_username=<root>   - the name of dst host
    #--dst_ip=<ip>           - dst host's ip
    #--dst_passwd=<passwd>   - dst host user's login passworld
    #--dst_port=<port>       - dst host's port of  open ssh (default 22) 
    #--dst_path=<path>       - dst host's port of  open ssh (default 22) 


    if [ -n "$src_username" ] && [ -n "$dst_username" ] ; then
        #firstly, cp file to local; second, cp local files to dst host
        src_base=`basename $src_path`
        dir_tmp=`mktemp -d`
        #echo "j1"
        src="$src_username@$src_ip:$src_path"
        dst="$dir_tmp"
        port=$src_port
        passwd=$src_passwd
        scp_copy_file "$src" "$dst" $port "$passwd"
        if [ $? -eq 0 ] ; then
            src="$dir_tmp/$src_base"
            dst="$dst_username@$dst_ip:$dst_path"
            port=$dst_port
            passwd=$dst_passwd
            scp_copy_file "$src" "$dst" $port "$passwd"
        fi
        if [ -d "$dir_tmp" ] ; then rm -r "$dir_tmp"; fi

    elif [ -n "$src_username" ] ; then
        src="$src_username@$src_ip:$src_path"
        dst="$dst_path"
        port=$src_port
        passwd=$src_passwd
        scp_copy_file "$src" "$dst" $port "$passwd"
    elif [ -n "$dst_username" ] ; then
        vlogd "src is: $src_path"
        src="$src_path"
        dst="$dst_username@$dst_ip:$dst_path"
        port=$dst_port
        passwd=$dst_passwd
        scp_copy_file "$src" "$dst" $port "$passwd"
    fi


    ret=$?   #mark return code


    exit $ret



